package geekTime.chapter23Future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 使用future实现烧水泡茶最优解
 * 最优解：线程1 洗水壶->烧开水->(等待t2完成后)泡茶
 *        线程2 洗茶壶->洗茶杯->拿茶叶
 * Created by LinGer on 2019/7/2
 */
public class FutureTaskTest {

    public static void main(String[] args) {
        FutureTask<String> ft2 = new FutureTask<>(new T2Task());
        FutureTask<String> ft1 = new FutureTask<>(new T1Task(ft2));

        //线程t1执行任务ft1
        Thread t1 = new Thread(ft1);
        t1.start();

        //线程t2执行任务ft2
        Thread t2 = new Thread(ft2);
        t2.start();

        //等待线程t1的结果
        try {
            System.out.println(".......: " + ft1.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
